package com.example.myapplication

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.Button
import android.widget.LinearLayout
import android.widget.Spinner
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    // 声明控件变量
    private lateinit var btnAddText: Button
    private lateinit var linearLayout: LinearLayout
    private lateinit var spinnerLanguage: Spinner
    private lateinit var btnConfirm: Button
    private lateinit var tvLanguage: TextView

    // 动态TextView计数器
    private var textCount = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 初始化控件
        initViews()

        // 设置功能区1：动态添加TextView
        setupDynamicTextViews()

        // 设置功能区2：语言选择功能
        setupLanguageSelection()
    }

    /**

     */
    private fun initViews() {
        btnAddText = findViewById(R.id.btnAddText)
        linearLayout = findViewById(R.id.linearLayout)
        spinnerLanguage = findViewById(R.id.spinnerLanguage)
        btnConfirm = findViewById(R.id.btnConfirm)
        tvLanguage = findViewById(R.id.tvLanguage)
    }
     */
    private fun setupDynamicTextViews() {
        btnAddText.setOnClickListener {
            // 创建新的TextView
            val dynamicTextView = TextView(this).apply {
                text = "动态文本 $textCount"
                textSize = 16f
                setPadding(
                    16.dpToPx(),
                    16.dpToPx(),
                    16.dpToPx(),
                    16.dpToPx()
                )
            }
            linearLayout.addView(dynamicTextView)

            // 计数器递增
            textCount++
        }
    }

    /**

     */
    private fun setupLanguageSelection() {
        // Spinner选择事件监听
        spinnerLanguage.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>,
                view: View?,
                position: Int,
                id: Long
            ) {
                // 获取选中的语言
                val selectedLanguage = parent.getItemAtPosition(position).toString()
                // 更新显示（未点击确认前的临时显示）
                tvLanguage.text = selectedLanguage
            }

            override fun onNothingSelected(parent: AdapterView<*>) {
                // 未选择时显示默认文本
                tvLanguage.text = getString(R.string.kotlin_text)
            }
        }

        // 确认按钮点击事件
        btnConfirm.setOnClickListener {
            // 获取最终选择的语言
            val confirmedLanguage = spinnerLanguage.selectedItem.toString()
            // 使用MVC模式的模型层处理数据
            val languageModel = LanguageModel()
            val languageInfo = languageModel.getLanguageInfo(confirmedLanguage)
            // 更新UI显示详细信息
            tvLanguage.text = languageInfo
        }
    }

    /**
     * 辅助函数：将dp转换为px，适配不同屏幕
     */
    private fun Int.dpToPx(): Int {
        return (this * resources.displayMetrics.density).toInt()
    }

    /**
     * MVC模式的模型层：处理语言信息业务逻辑
     */
    inner class LanguageModel {
        fun getLanguageInfo(language: String): String {
            return when (language.lowercase()) {
                "kotlin" -> "Kotlin：现代Android开发首选语言，简洁安全"
                "java" -> "Java：传统Android开发语言，生态成熟"
                "python" -> "Python：多用途语言，适合数据分析和AI"
                "swift" -> "Swift：iOS开发语言，性能优异"
                else -> "未知语言，请选择列表中的选项"
            }
        }
    }
}
